package com.itlong.cloud.operate.service.impl;

import com.itlong.cloud.POJO.DTO.operate.OperateProjectUpdateDTO;
import com.itlong.cloud.POJO.DTO.operate.PropertyFinanceManageGetPageDTO;
import com.itlong.cloud.POJO.PO.ProjectPO;
import com.itlong.cloud.POJO.VO.operate.OperateGetProjectFinancePageVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.operate.dao.IPropertyFinanceManageDao;
import com.itlong.cloud.operate.service.IPropertyFinanceManageService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <desc>
 *      运营-物业财务管理服务接口实现类
 * <desc/>
 *
 * @createDate 2018/09/20
 */
@Service
public class PropertyFinanceManageServiceImpl implements IPropertyFinanceManageService {

    @Autowired
    private IPropertyFinanceManageDao iPropertyFinanceManageDao;

    /**
     * <desc>
     *      物业财务管理-分页获取项目信息
     * </desc>
     *
     * @param propertyFinanceManageGetPageDTO 运营-物业财务管理项目列表查询DTO
     * @return 分页结果
     * @author wangzhi
     * @createDate 2018/09/20
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<OperateGetProjectFinancePageVO> getProjectFinancePage(PropertyFinanceManageGetPageDTO propertyFinanceManageGetPageDTO) throws Exception {
        Map<String,Object> params = SqlUtil.convertBeanToMapParams(propertyFinanceManageGetPageDTO);
        Page<OperateGetProjectFinancePageVO> page = new Page<>();
        page.getPage().setPageSize(propertyFinanceManageGetPageDTO.getPageSize());
        page.getPage().setCurrentPage(propertyFinanceManageGetPageDTO.getCurrentPage());
        params.put("startSlip", page.getPage().getStartSlip());
        params.put("pageSize", page.getPage().getPageSize());
        List<OperateGetProjectFinancePageVO> operateGetProjectFinancePageVOS = iPropertyFinanceManageDao.getProjectFinancePage(params);
        page.setRows(operateGetProjectFinancePageVOS,iPropertyFinanceManageDao.getProjectFinanceRecordCount(params));
        return page;
    }

    /**
     * <desc>
     *      获取所有项目信息
     * </desc>
     *
     * @param propertyFinanceManageGetPageDTO 条件
     * @return 项目信息列表
     * @author wangzhi
     * @createDate 2018/09/20
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<OperateGetProjectFinancePageVO> getProjectFinanceList(PropertyFinanceManageGetPageDTO propertyFinanceManageGetPageDTO) throws Exception{
        Map<String,Object> params = SqlUtil.convertBeanToMapParams(propertyFinanceManageGetPageDTO);
        return iPropertyFinanceManageDao.getProjectFinanceList(params);
    }

    /**
     * <desc>
     *      财务启用或禁用项目
     * <desc/>
     *
     * @param updateDTO 项目更新信息
     * @return 受影响的行数
     * @author wangzhi
     * @createDate 2018/10/24
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer updateProject(OperateProjectUpdateDTO updateDTO) throws Exception {
        ProjectPO projectPO = new ProjectPO();
        projectPO.setProjectId(updateDTO.getProjectId());
        projectPO.setFinanceStatus(updateDTO.getStatus());
        return iPropertyFinanceManageDao.updateProject(SqlUtil.durableData(projectPO,PlatformConstants.TABLE_UPDATE));
    }

}
